$(function () {
    validateData();
    initLayerDate("#startDate",true);
    initLayerDate("#endDate",true);
    $("body").on('click','#selectCourse',function () {//选择课程
        selectCourse({
            selectType:"checkbox",
            type:1,
            callback:'selectCourseCallback'
        });
    });
    inputRadioSel($(".i-checks"),selectCall);//查询所有单选按钮，回调方法selectCall
    $('body').on('click', '#submitGiftCard', function () {
        saveUpdateGiftCard();
    });
    $('body').on('click','#courseName i.fa-remove',function () {
        var dataId = $(this).parents('li').data('id');
        var  obj =$(this).parents('li');
        deleteCourse(dataId,obj);
    });
    $('body').on('click', '#onBack', function () {
        onBackHistory();
    });


});//初始化完成
/**
 * 取消
 */
function onBackHistory() {
    window.history.go(-1);
}

/**
 * 选择回调
 */
function selectCall(em) {
    if(em.name=='timeType'){
        if(em.value==1){
            $('.timeDays').hide();
            $('.beginTimeAndEndTime').show();
        }
        if(em.value==2){
            $('.timeDays').show();
            $('.beginTimeAndEndTime').hide();
        }
    }
    if(em.name=='cardType'){
        if(em.value==1){
            $('.moneyCard').hide();
            $('.courseCard').hide();
            $('.myExchange').hide();
            $('input[name="isExchange"]').iCheck('uncheck');
        }
        if(em.value==2){
            $('.courseCard').show();
            $('.moneyCard').hide();
            $('.myExchange').show();
        }
        if(em.value==3){
            $('.moneyCard').show();
            $('.courseCard').hide();
            $('.myExchange').show();
        }
        $("#giftCardCardType").val(em.value);
    }
}


/**
 * 选择课程回调
 * @param result 回调数据
 */
function selectCourseCallback(result) {
    if($("#courseName").find("li").length>=3||result.length>3){
        icModale(0,"课程卡最多设置三个课程");
        return;
    }
    if(result!=null && result.length>0){
        var courseIds = $('input[name="courseIds"]').val();
        if(courseIds==null||courseIds==''){
            courseIds =',';
        }
        $.each(result,function (index,course) {
            var courseName =course.courseName;
            if(courseIds.indexOf(","+course.id+",")==-1){
                courseIds +=course.id+',';
                $("#course-div").show();
                $('#courseName').append('<li class="list-group-item"  data-id="'+course.id+'" >'
                    +'<span class="pull-right">'
                    +'<a href="javascript:void(0);" title="移除" class="c-999 fs14"><i class="fa fa-remove" ></i></a>'
                    +'</span>'
                    +'<span class="fs14">'
                    +'<i class="fa fa-book"></i>'+ courseName
                    +'</span>'
                    +'</li>');
            }
        });
        $('input[name="courseIds"]').val(courseIds);
    }
    parentId = 0;
}
/**
 * 删除选中课程
 */
function deleteCourse(dataId,obj) {
    var courseIds=',';
    var courses = $("#courseIds").val();
    var courseId = courses.split(',');
    for(var i=0;i<courseId.length;i++){
        if(courseId[i]!=null&&courseId[i]!=''&&courseId[i]!=dataId){
            courseIds +=courseId[i]+",";
        }
    }
    if(courseIds.length>1){
        $("#courseIds").val(courseIds);
    }else{
        $("#courseIds").val("");
    }
    obj.remove();
}

/**
 * 新建礼品卡
 * @param status
 */
function saveUpdateGiftCard() {
    var giftCardName=$('input[name="cardName"]').val();
    var giftCardGenNum=$('input[name="genNum"]').val();
    var giftCardTimeType=$('input[name="timeType"]:checked').val();
    var giftCardTimeDays=$('input[name="timeDays"]').val();
    var giftCardCardType =$("#giftCardCardType").val();
    if (giftCardName.trim() == ''){
        return icModale(0,"请输入礼品卡名称");
    }
    if(giftCardCardType==3){
        if(!isNumber($('input[name="amount"]').val())||$('input[name="amount"]').val()==0){
            return icModale(0,"请正确添加礼品卡金额");
        }
    }
    if(!isNumber(giftCardGenNum)||giftCardGenNum==0){
        return icModale(0,"请正确添加礼品卡数");
    }
    if(giftCardGenNum>10000){
        return icModale(0,"礼品卡数最多只能生成一万张");
    }
    if(giftCardTimeType==2){
        if(!isNumber(giftCardTimeDays)||giftCardTimeDays==0){
            return icModale(0,"请正确添加天数");
        }
    }
    var params = $("#giftCardCreateFrom").serializeJSON();
    params = JSON.stringify(params);
    $.ajax({
        url: baseloaction + "/admin/op/giftCard/saveOpGiftCard",
        data: params,
        dataType:"json",
        type: "post",
        contentType:'application/json;charset=utf-8',
        beforeSend:function (XHR) {
            icModale(0,'数据保存中，请耐心等待...');
            $("#submitGiftCard").attr({ disabled: "disabled" });
            return true;
        },
        success: function (result) {
            if(result.success==true){
                icModale(0,result.message);
                window.location.href=baseloaction+"/admin/op/giftCard/toOpGiftCardListPage";
            }else{
                $("#submitGiftCard").prop("disabled",false);
                icModale(0, result.message);
            }
        },
        error:function (error) {
            $("#submitGiftCard").prop("disabled",false);
            icModale(0, '系统错误，数据加载失败');
        }
    });
}
